/**
 * 页面初始化
 */
$(function () {
    //异步请求产品类型
    $.ajax({
        type: 'get',
        url: '/productMenu',
        data: {},
        success: function (data) {
            // console.log(data.list);
            if (data.message == "success") {
                if (data.total > 0) {
                    let num = 0;
                    $(data.list).each(function (index,item) {
                        num++;
                        let productNav = "<div >\n" +
                            "                    <a href=\"#\" onclick=\"findById("+item.id+",'/html/product_display.html')\">\n" +
                            "                        <span>"+item.name+"</span>\n" +
                            "                    </a>\n"+
                            "<div class=\"dropdown_"+num+"column\">\n" +
                            "                        <div class=\"col_1\">\n" +
                            "                            <ul class=\"simple\">";
                        let id = item.id;
                        if (item.productMenus.length > 0) {
                            $(item.productMenus).each(function (index,item) {
                                if (id == item.parentId) {
                                    productNav += "<li><a href=\"#\" onclick=\"findById("+item.id+",'/html/product_display.html')\">"+item.name+"</a></li>";
                                }
                            });
                        }
                            productNav += "</ul>\n" +
                                "                        </div>"+
                                "                    </div>\n" +
                                "                </div>";
                        $("#product-nav").append(productNav);
                    })
                }
                return false;
            }
            layer.msg('查询失败，请稍后重试！', {icon: 2, time: 1200});
            return false;
        },
        error: function () {
            layui.layer.msg('请求接口异常,请刷新重试！',{icon:0,time:1300});
            return false;
        }
    })

//    分页查询产品
    pageFindAll(1,8);

})

/**
 * 分页查询产品
 * @param page
 * @param limit
 */
function pageFindAll(page,limit) {
    // 产品类型，父或子类型
    var tMenuId = sessionStorage.getItem("id");
    let data = {'page':page,'limit':limit};
    if (tMenuId != null) {
        data = {'page':page,'limit':limit,'tMenuId':tMenuId};
    }

    //异步请求产品类型
    $.ajax({
        type: 'get',
        url: '/productDisplay',
        data: data,
        success: function (data) {
            // console.log(data.list);
            if (data.message == "success") {
                if (data.total > 0) {
                    $("#product-list").html("");
                    $(data.list).each(function (index, item) {
                        let product = "<li>\n" +
                            "                            <div>\n" +
                            "                                <a href=\"#\" onclick=\"findById(" + item.id + ",'/html/product_details.html')\" class=\"img-href\">\n" +
                            "                                    <img src=\"" + item.img + "\" />\n" +
                            "                                    <span>" + item.name + "</span>\n" +
                            "                                </a>\n" +
                            "                                <a href=\"#\" onclick=\"findById(" + item.tMenuId + ",'/html/product_display.html')\" class=\"img-type\">\n" +
                            "                                    " + item.tMenuName + "\n" +
                            "                                </a>\n" +
                            "                                <p class=\"product-price\">价格：<span>" + item.platformPrice + "</span></p>\n" +
                            "                            </div>\n" +
                            "                        </li>";

                        $("#product-list").append(product);
                    });
                    // 分页
                    $("#product-pages").html("");
                    //上一页
                    let pageNum = data.pageNum > 0 ? data.pageNum - 1 : 0;
                    // console.log("上一页="+pageNum);
                    let newsPages = "<div>\n" +
                        "                    <a href=\"\" class=\"page-left\" onclick='pageFindAll(" + pageNum + ",8)'>《</a>\n" +
                        "                </div>";
                    $("#product-pages").append(newsPages);
                    //中间数字页码
                    for (let i = 1; i <= data.pages; i++) {
                        let newsPages = "<div>\n" +
                            "                    <a href=\"#\" class=\"page-center\" onclick=\"pageFindAll(" + i + ",8)\" >" + i + "</a>\n" +
                            "                </div>";
                        $("#product-pages").append(newsPages);
                    }
                    //下一页
                    let pageRight = data.pageNum >= data.pages ? data.pages : data.pageNum + 1;
                    // console.log("下一页="+pageRight);
                    let newsPage = "<div>\n" +
                        "                    <a href=\"#\" class=\"page-right\" onclick='pageFindAll(" + pageRight + ",8)'>》</a>\n" +
                        "                </div>";
                    $("#product-pages").append(newsPage);
                    let page = "<div>\n" +
                        "                    <span>当前页码：</span>\n" +
                        "                    <span>" + data.pageNum + "</span>\n" +
                        "                </div>";
                    $("#product-pages").append(page);
                    return false;
                } else {
                    $("#product-list").html("暂无产品！");
                    return false;
                }
            }
            layer.msg('查询失败，请稍后重试！', {icon: 2, time: 1200});
            return false;
        },
        error: function () {
            layui.layer.msg('请求接口异常,请刷新重试！',{icon:0,time:1300});
            return false;
        }
    });

}